0

Have code in alone sript for ajax including,

   $connection = mysqli_connect($db_config['server'],$db_config['db_user'],$db_config['db_pass']);
if (!$connection)
    die('Could not connect to database: ' . mysql_error());
mysqli_select_db($connection, $db_config['db_name']);

$test = mysql_query("SELECT * FROM chat");
echo "test: <br>";
print_r($test);

but it returns nothig, if i call:

//db connect
$connection = mysqli_connect($db_config['server'],$db_config['db_user'],$db_config['db_pass']);
if (!$connection)
    die('Could not connect to database: ' . mysql_error());
mysqli_select_db($connection, $db_config['db_name']);

$test = mysql_query("SELECT * FROM chat");

print_r($connection);

returns:

mysqli Object ( [affected_rows] => -1 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 40933630edef551dfaca71298a83fad8d03d62d4 $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 0 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.13-log [server_version] => 50613 [stat] => Uptime: 7999358 Threads: 4 Questions: 656648624 Slow queries: 9090 Opens: 3348364 Flush tables: 1 Open tables: 1999 Queries per second avg: 82.087 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 5759563 [warning_count] => 0 )

Martin
  • 2,575
  • 6
  • 32
  • 53

3 Answers3

3

you connected by mysqli and you used mysql.

change that

  $test = mysql_query("SELECT * FROM chat");

to

 $test = mysqli_query($connection,"SELECT * FROM chat");

and then you need to fetch your query like that

 $row= mysqli_fetch_array($test) ;
 echo $row['some_column'];

this is whole code:

 $connection =mysqli_connect($db_config['server'],$db_config['db_user'],$db_config['db_pass']);
 if (!$connection)
  die('Could not connect to database: ' . mysql_error());
    mysqli_select_db($connection, $db_config['db_name']);

  $test = mysqli_query("SELECT * FROM chat");
  $row= mysqli_fetch_array($test) ;
  echo $row['some_column']."<br />"; //you need to put the column you want to output.
echo_Me
  • 37,078
  • 5
  • 58
  • 78
2

You are using mysqli_connect, but mysql_query. You can't mix mysqli and mysql - use one or the other. Most people will tell you to use mysqli. I'd say to use whichever is best for you. In my case, that's mysql because I don't have to pass a connection reference to every single call... among other reasons.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 1
    Correct source of the problem, but telling him to go with `mysql_*` is a terrible idea. `mysql_*` is deprecated and should not be added to newly written code. Go with `mysqli_*`, then you'll also possibly have less to change later if you go with the object oriented approach, or start using prepared statements. – Thorbear Feb 22 '14 at 12:38
  • @Thorbear Fair. Personally I use a DB class that I built, which wraps `mysql_` functions. Very easy to change, since I just have to change that one class and I'm done ^_^ – Niet the Dark Absol Feb 22 '14 at 14:28
0

As mysql is depricated try using mysqli, pdo or any other. Code for mysqli is as follow:

<?php
$mysqli = new mysqli("host", "username", "password", "database_name");

$test= $mysqli->query("SELECT * FROM chat") or die($mysqli->error);

if($test->num_rows){
            while($rows = $test->fetch_array(MYSQLI_ASSOC)){
                $data1= $rows['data1'];
                $data2= $rows['data2'];
            }
}
?>

$test->num_rows checks for number of results and while loop is used to get all the data returned.

Bhupesh Shrestha
  • 248
  • 3
  • 17