0

I am trying to test if this query runs or not.But i am output with blank screen i.e no output. I am using xampp server.

<?php
$mysql_host='localhost'; 
$mysql_user='root';
$mysql_pass='';
$mysql_db='a_database';
$con_error='connection error';
$query="SELECT `name`  FROM `users` WHERE `id`='1' "; //selecting name from databas where id is 1
$query_run=mysql_query($query); //this is my query 

if($query_run)
  {

   echo 'success';
  }

?>

Please help me with this. $query_run neither returns false here nor true. I am not able to understand where the problem is.

Till Helge
  • 9,253
  • 2
  • 40
  • 56
user18683
  • 35
  • 6
  • have you established the connection to your database.? – 웃웃웃웃웃 Feb 05 '14 at 12:17
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Feb 05 '14 at 12:20

3 Answers3

2

First of all try to avoid mysql_* functions from php > 5.4, use mysqli_* function like this. connect to Databse before running a query like this

$con=mysqli_connect("localhost","my_user","my_password","my_db"); 
$query="SELECT `name`  FROM `users` WHERE `id`='1' "; //selecting name from databas where id is 1
$query_run=mysqli_query($con,$query); 

For php < 5.5 use this

$con=mysql_connect("localhost","my_user","my_password","my_db"); 
$query="SELECT `name`  FROM `users` WHERE `id`='1' "; //selecting name from databas where id is 1
$query_run=mysql_query($con,$query); 
krishna
  • 4,069
  • 2
  • 29
  • 56
1

First of all stop using mysql it is deprecated. Use mysqli now.

In your script you missed the connection to database. Add before your query:

$link = mysqli_connect($mysql_host,mysql_user,$mysql_pass,$mysql_db) or die("Error " . mysqli_error($link));

For more details see this link.

user2936213
  • 1,021
  • 1
  • 8
  • 19
0

try this:::

<?php

$link = mysqli_connect("localhost","root","root","a_database") or die("Error " . mysqli_error($link));

$query = "SELECT name FROM users" or die("Error in the consult.." . mysqli_error($link));
$result = $link->query($query);

while($row = mysqli_fecth_array($result)) {
  echo $row["name"] . "<br>";
} 
?>

as mentioned there, use mysqli_*

Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61