carleihar Posted November 14, 2009 Share Posted November 14, 2009 I recently asked how to locate a single cell in mysql and assign it to a variable and I was told to do it like so: $sql = "SELECT color FROM horses WHERE horse_id=$momhorseid LIMIT 1"; $row = mysql_fetch_assoc($query); $momcolor= $row_mcolor['color']; My question is, is there a way to do this same thing with mysqli? Thanks! Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/ Share on other sites More sharing options...
thepip3r Posted November 14, 2009 Share Posted November 14, 2009 i would imagine u would use the same sql query with code examples from the manual for: http://us2.php.net/manual/en/mysqli-result.fetch-assoc.php Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/#findComment-957182 Share on other sites More sharing options...
trq Posted November 14, 2009 Share Posted November 14, 2009 The code you have posted doesn't actually do anything, you never execute your $sql statement. Maybe you just left it out. Anyway.... $db = new mysqli(); $sql = "SELECT color FROM horses WHERE horse_id=$momhorseid LIMIT 1"; if ($result = $db->query($sql)) { if ($result->num_rows)) { $row = $result->fetch_assoc(); $momcolor= $row['color']; } } Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/#findComment-957185 Share on other sites More sharing options...
carleihar Posted November 14, 2009 Author Share Posted November 14, 2009 Now I am getting the error: An error occurred in script '/home/content/e/q/u/equianadmin/html/htdocs/pages/practice.php' on line 15: mysqli::query() [mysqli.query]: Couldn't fetch mysqli $db = new mysqli(); $sql = "SELECT color FROM horses WHERE horse_id=$momhorseid LIMIT 1"; if ($result = $db->query($sql)) { if ($result->num_rows) { $row = $result->fetch_assoc(); $momcolor= $row['color']; } } echo "$momcolor"; Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/#findComment-957193 Share on other sites More sharing options...
thepip3r Posted November 14, 2009 Share Posted November 14, 2009 have you enabled the mysqli extension in your PHP config file? Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/#findComment-957196 Share on other sites More sharing options...
trq Posted November 14, 2009 Share Posted November 14, 2009 You need to actually put your connection details within the call to mysqli_connect. This is all covered pretty extensively in the manual. Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/#findComment-957199 Share on other sites More sharing options...
carleihar Posted November 14, 2009 Author Share Posted November 14, 2009 This is whats in my mysqli_connect.php file (with it filled out respectively and correctly): DEFINE ('DB_USER', 'username); DEFINE ('DB_PASSWORD', 'pass.'); DEFINE ('DB_HOST', 'host'); DEFINE ('DB_NAME', 'databasename'); // Make the connection: $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (!$dbc) { trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() ); } Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/#findComment-957224 Share on other sites More sharing options...
trq Posted November 14, 2009 Share Posted November 14, 2009 And the exact error you are now getting? Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/#findComment-957227 Share on other sites More sharing options...
carleihar Posted November 14, 2009 Author Share Posted November 14, 2009 An error occurred in script '/home/content/e/q/u/equianadmin/html/htdocs/pages/practice.php' on line 15: mysqli::query() [mysqli.query]: Couldn't fetch mysqli Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/#findComment-957245 Share on other sites More sharing options...
PHPFreaksMaster Posted November 14, 2009 Share Posted November 14, 2009 try this code: DEFINE ('DB_USER', 'username'); DEFINE ('DB_PASSWORD', 'pass.'); DEFINE ('DB_HOST', 'host'); DEFINE ('DB_NAME', 'databasename'); // Make the connection: $mysqli = @new @mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $strQuery = "SELECT color FROM horses WHERE horse_id=$momhorseid LIMIT 1"; $query = $mysli->query($strQuery); if($query){ if($query->num_rows>0){ $result = $query->fetch_assoc(); }else{ echo "No record found"; } }else{ die($mysqli->error); } Note: You must change your hostname, username, password and databasename according to your server information. Link to comment https://forums.phpfreaks.com/topic/181452-mysqli-single-cel/#findComment-957267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.