1

Firstly I am randomly selecting the ID from my table. That part works fine but the next part doesn't. The next part is selecting the ID's row, e.g. if the ID is 6, then it should select all the fields related to 6.

my table is like this:

------------------------------
|ID|Name|Email      |Password|
------------------------------
|1 |Amy |[email protected]|jaaaaaaa|
------------------------------
|2 |Bob |[email protected]|haaukanm|
------------------------------
|3 |Bill|[email protected]|fsoji443|
------------------------------

This is my code:

<?php
include('connect.php');

//select a number between min id and max id
$v = "SELECT ID FROM `tblaccounts` ORDER BY RAND() LIMIT 1";
$result = mysqli_query($connection, $v);
$data2 = mysqli_fetch_array($result);
//var_dump($data2);

$c = "SELECT * FROM `tblaccounts` WHERE ID='$data2'";
$cresult = mysqli_query($connection, $c);
$data3 = mysqli_fetch_array($cresult);
var_dump($data3);
?>

3 Answers 3

4

The issue is here:

$c = "SELECT * FROM `tblaccounts` WHERE ID='$data2'";

here $data2 is not a single value, its an array and you are trying to compare it in WHERE like a string, that's why the error. Instead try $data2['id'] like:

$c = "SELECT * FROM `tblaccounts` WHERE ID='".$data2['ID']."'";

or

$c = "SELECT * FROM `tblaccounts` WHERE ID=".$data2['ID'];  // Sinlge quote is not required if `ID` is `int`
Sign up to request clarification or add additional context in comments.

Comments

1

Because your $data2 is an array, this is should work

include('connect.php');

//select a number between min id and max id
$v = "SELECT ID FROM `tblaccounts` ORDER BY RAND() LIMIT 1";
$result = mysqli_query($connection, $v);
$data2 = mysqli_fetch_array($result);

//var_dump($data2);
$c = "SELECT * FROM `tblaccounts` WHERE ID='".$data2['ID']."'";
$cresult = mysqli_query($connection, $c);
$data3 = mysqli_fetch_array($cresult);
var_dump($data3);

Comments

0

You are getting this error as you are comparing array in where clasue. Your $data is an array like below

$data = array(
             'ID'=>2
             'Name'=>'Bob',
             'Email'=>'[email protected]',
             'Password'=>'haaukanm'

       );// say record with id 2 is fecthed

So use $data['ID'] in your where clause

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.