I’m trying to call a database for the first time in PHP, and this query is causing my code to break. Note that I’ve tested the connection to be good. The culprit is mysql_query(). Can anybody spot what might be going wrong? The table name is “users” and the entry under the ‘Name’ column is ‘mvalentine’. Everything matches case as far as I can tell.
dbInit.php
<?php
$connection = mysql_connect('localhost', 'root', 'password');
$db = mysql_select_db('scaleup');
if ($db) {
$user = mysql_query("SELECT ID FROM 'users' WHERE 'Name' = 'mvalentine'");
}
else {
die ('Error 01: Connection to database failed.');
}
?>
This modified code is now returning something. The value ‘users’ in the ajax call is now returning “false”
The value being returned should be ‘1’
ajax response:
<?php
include('dbInit.php');
include('objects.php'); //irrelevant, all code working properly
$layout = new Layout();
$bids = new Bids();
$out = array('layout' => $layout->_board, 'height' => $layout->_height, 'width' => $layout->_width,
'bids' => $bids->_board, 'maxBids' => $bids- >_maxBids, 'users' => $user);
$out = json_encode($out);
echo $out;
?>
It seems like you are expecting $user to contain the user ID, but it will actually contain a resource containing all of the rows returned. In order to get the user ID, you will need something like this:
Also, take note of the other comments and answers regarding style and the preference of mysqli and PDO for this type of thing.