Detective merry Detective merry - 18 days ago 5
MySQL Question

PHP query result into variable

Hi everyone I am a beginner programmer trying to get query results into variable in PHP. For registeration purposes, I want to assign an available campaignID to variable $campaignID, so I can insert $campaignID along with other variables to the new user row. I only need help on getting my result of my SELECT Statement into the variable campaignID! I am aware that there are multiple questions asking the exact thing want like 1 & 2. However, I have been trying the solutions for hours now but to no avail and I need help to apply the answers to my situation.

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ERROR);
try{
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$query = "SELECT campaignID from ebaycampaigns WHERE Taken='N' LIMIT 1";
$result = $mysqli->query($query);

if(!$result){
echo "No data was selected";
}

if (mysqli_num_rows($result)==1){
$row = mysql_fetch_array($result);
echo "campaignID: " . $row[0];

//or
//$row = $result->fetch_array(MYSQLI_BOTH);
//printf ("%s (%s)\n", $row['campaignID']);

}

else{
echo "not found!";
}

$conn->close();
}
catch(Exception $e) {
$json_out = "[".json_encode(array("result"=>0))."]";
echo $json_out;

}

?>


None of the responses were echoed,
$row['campaignID']
,
$row[0]
,
$campaignID
. I am also not conerned with SQL injections so far as this is a small project! So feel free to recommend any methods!

Answer

First off change mysql_fetch_array to mysqli_fetch_array :)

nevermind the following line, iil just leave it in, I saw you used LIMIT.

And secondly change mysqli_num_rows($result)==1 to mysqli_num_rows($result) > 0