Jump to content

mysqli Single Cel


carleihar

Recommended Posts

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.