php look in database for data not working - php

i'm making a quiz webpage connected to a datbase, but my code isn't working.
i have this code:
$query = "SELECT `question` FROM `questions` WHERE `id` = '1'";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$question = mysql_result($result, 0);
echo $question;
it should look in my table for a question with id 1 and then echo the question, the possible answers and the correct answer.
the column names are: id, question, a, b, c, d and correct
here is how it looks like in phpmyadmin,
since im not allowed to post images, here is a link to the image.
http://i.stack.imgur.com/tNJkd.png
can someone help me???

Try This
$query = "SELECT `question` FROM `questions` WHERE `id` = '1'";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$question = mysql_result($result, 0);
echo $question['question'];

$question = mysql_result($result, 0);
echo $question['question'];
OR
$question = mysql_result($result, 0, 'question');
echo $question;
should do.
Aside of that, mysql_*-functions are deprecated

try this
$query = "SELECT `question` FROM `questions` WHERE `id` = '1'";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$question = mysql_result($result, 0, 'question');
echo $question;

Try This
$query = "SELECT `question` FROM `questions` WHERE `id` = '1'";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$question = mysql_fetch_assoc($result);
echo $question['question'];
/*
or for get the correct answer
you can just: echo $question['correct'];
//*/

Related

Array to string conversion in mysql while loop

Following is my code,
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
When I'am trying to use $myArrayOfemp_id variable in $sql query, It shows that error:
Array to string conversion in..
How can I fix it?
You are trying to convert an array into a string in the following line:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$myArrayOfemp_id is an array. That previous line of code should be changed to:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id={$myArrayOfemp_id[0]} ORDER BY apply_date DESC";
I placed 0 inside {$myArrayOfemp_id[0]} because I'm not sure what value want to use that is inside the array.
Edited:
After discussing what the user wanted in the question, it seems the user wanted to use all the values inside the array in the sql statement, so here is a solution for that specific case:
$sql = "SELECT emp_id FROM emp_leaves
WHERE ";
foreach ($myArrayOfemp_id as $value)
{
$sql .= " emp_id={$value) || ";
}
$sql .= "1=2";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id in
(SELECT GROUP_CONCAT(emp_id) FROM employee where manager_id=".$userID.")
ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
just change your query like above might solve your problem.
you can remove following code now. :)
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);

mysql dosen't select all when using the SELECT function

My code looks like this:
$sql = "SELECT `sw1`, `sw2`, `sw3`, `sw4`, `fb1`, `fb2`, `fb3`, `fb4`, `bew1`, `bew2`, `bew3`, `bew4` FROM `reg` WHERE `id` = ".$id." ORDER BY `id` ASC LIMIT 0, 30 ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
foreach($row as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
}
}
There is some data in sw2 but it isn't shown.
When I tryed to UPDATE data the data in the db wasn't changed.
$id is right.
other data from the table could be read.
This code works fine:
$sql = "SELECT * FROM `reg` WHERE `id` = ".$id." ORDER BY `id` ASC LIMIT 0, 30 ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
print_r($row);
}
Try your query with echo.
same trouble follows try changing Where 'id' = "$id"
to Where 'id' like "$id"
You dont need quotes:
$sql = "SELECT sw1, sw2, sw3, sw4, fb1, fb2, fb3, fb4,
bew1, bew2, bew3, bew4
FROM reg WHERE id = $id";

process sql query results

I got a table named "Serials" with 5 comumns
Serial, Code, Name, Redeemed, Redeem_date
i am selecting some fields from that table with this query:
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$db->query();
But i dont know how to pass these values in the following variables so i can use them in if statements later
$name= //retured value from column Name
$redeemed= //retured value from column Redeemed
$redeem_date= //retured value from column Redeem_date
just like this..
// Your query here..
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$results = $db->query();
//fetch data and stored into variables
while($row = fetch_array($results)){
$name = $row['Name'];
$redeemed = $row['Redeemed'];
$redeem_date = $row['Redeem_date'];
}
try something like this :
<?php
$result = $db->query("SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'");
while (list($name, $redeemed, $redeem_date) = $result->fetch(PDO::FETCH_NUM)) {
// DO SOMETHING
}
?>
while ($row = $db->fetch()) {
$name= $row['name'];
$redeemed= $row['redeemed'];
$redeem_date= $row['redeem_date'];
}
this one might fetch your results and assign to vars

How to create a OR statement in one query?

How do i make a OR query for something like this?
//Query
$sql = "select `Friend_status` from `friendship_box` where
`system_id` = '$sid' and `friend_id` = '$friendis' OR
`system_id='$friendis' and `friend_id` = '$sid'";
$query = mysql_query($sql) or die ("Error: ".mysql_error());
while ($row = mysql_fetch_array($query)){
$activity = htmlspecialchars($row['Friend_status']);
}
mysql_free_result($query);
select `Friend_status` from `friendship_box`
where (`system_id` = '$sid' and `friend_id` = '$friendis')
OR (`system_id`='$friendis' and `friend_id` = '$sid')
you missed the ` here:
...OR (`system_id` <--

Product ID is not being stored in the database

can anyone help? my code doesn't seem to store the value of product id here in my code have a look I am also getting the ID from another table
<?php
include("Connection.php");
$dTime = time();
$myValue = $_REQUEST['dValue'];
echo "<p>
The time is: {$dTime}<br/>
The choice is {$myValue}
</p>
";
$sql = "Select ID from product where NAME = '$myValue'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
$pid=$row["PRODUCT_ID"];
$sql2 = "INSERT INTO `starbucks`.`order_details` (
`ID` ,
`ORDER_ID` ,
`PRODUCT_ID` ,
`QTY`
)
VALUES (
NULL , '', '$pid', '1'
)";
$result2 = mysql_query($sql2);
?>
updated the code
$id = $row["ID"]
instead of:
$id = $row;
You have an incorrect array value for $id instead of the array's ID key:
$id = $row;
// Should be
$id = $row['ID'];
in your original code there is no error handling,you should do something like this:
$sql = "Select ID from product where NAME = '$myValue'";
if ($sql) {
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
$pid = $row["PRODUCT_ID"];
$sql2 = "INSERT INTO `starbucks`.`order_details` (
`ID` ,
`ORDER_ID` ,
`PRODUCT_ID` ,
`QTY`
)
VALUES (
NULL , '', '$pid', '1'
)";
$result2 = mysql_query($sql2);
if (!$result2) {
echo mysql_error();
break;
}
} else {
echo mysql_error();
}
And see what error you get.

Categories