Detective merry Detective merry - 1 year ago 90
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.

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

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

echo "No data was selected";

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

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


echo "not found!";

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



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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download