Creating Staff Listing - php

I'm pretty sure I'm missing something incredibly simple. I want to create a table of my staff members using PHP/MySQL and display in an HTML table. I've gotten as far as writing the query and trying to fetch the array, but it keeps returning a bool variable. Where have I gone wrong?
<? $memberQuery = "SELECT * FROM members;";
$memberArray = mysql_fetch_array(mysql_query($memberQuery) or die("error retrieving members"));
var_dump($memberArray); ?>
The var_dump() returns bool(false), so I'm a bit lost...

You have an error in your code:
var_dump($memberArray);
instead of
var_dump($member array);

Try :
$sqlArray = mysql_query($memberQuery) or die(mysql_error ());
it will give some information about your problem.
If there is no error you will be able to visit your array with
$memberArray = mysql_fetch_query ($sqlArray);

Related

sqlite3 & PHP results empty: object(SQLite3Result)#3 (0) { }

Im starting to use sqlite3 but I'am unable to get some results.
My database is called database.sqlite3
Inside SQLite command prompt and doing 'SELECT * FROM table'; . works fine. But im having problems with php.
$db = new SQLite3('db/database.sqlite3');
$result = $db->query('SELECT * FROM table');
var_dump( $result );
The result of this code is : object(SQLite3Result)#3 (0) { }
What is worng here?
Thank you.
PD: $result->fetchArray(SQLITE3_ASSOC) works but only gives me one record... why???
from the php doc, the return value of query:
Return Values
Returns an SQLite3Result object, or FALSE on failure.
Have a look at the fetcharray method of the Result object to see how to get (fetch) the the row data.
So just in case someone is in the same trouble. SQLITE does not work like that. You must walk through a loop to get every row. Then you can create your data array.
Something like this.
$db = new SQLite3('db/database.sqlite3');
$result = $db->query('SELECT * FROM table');
while( $row = $result->fetchArray() ){
echo $row['name'];
}

How to combine multiple arrays obtained from mysql queries and then traverse through the new array

So I'm trying to build an image uploading website and wish to access a mysql table using a query ..now I wish to store all the arrays obtained from these queries into one single array. I then wish to access all the elements of this array. How should I do it?
This is what I tried:
$allimages = array();
$sql="SELECT uploaderId FROM foostable WHERE foo='bar'";//a query which fetches the image uploader's id from the foostable ..you don't need to worry about this part just know that this query returns more than one uploaderIds
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($result)){
$uploaderId=$row['uploaderId'];
$sql1="SELECT uploader FROM imagestable WHERE uploaderId='$uploaderId' ORDER BY datetime DESC";
$result1=mysqli_query($conn, $sql1);
$row1=mysqli_fetch_assoc($result1);
$allimages=$allimages+$row1;
}
foreach ($allimages as $ai) {
echo $ai['uploader'];
}
When I run this code, I get the following error:
Warning: Illegal string offset 'uploader' in...
I'm definitely doing something wrong but am not able to figure out what that is.
I've been looking everywhere for this but am not able to find it therefore I posted this question! I'm really new to this and any help would be really really appreciated! Thank you! :)
You're adding new elements to your array the wrong way.
Change
$allimages=$allimages+$row1;
to
$allimages[] = $row1; // Short for array_push($allimages, $row1)
Read more about array_push() in the manual
SELECT i.uploader
FROM foostable f
JOIN imagestable i
ON i.uploaderid = f.uploaderId
WHERE f.foo = 'bar'
ORDER
BY i.datetime DESC
You could write a single query to obtain the desired results. Please try the following code:
$allimages = array();
$sql = "SELECT i.uploader, i.datetime
FROM imagestable i INNER JOIN foostable f
ON i.uploaderId = f.uploaderId
AND f.foo = 'bar'
ORDER BY i.datetime DESC";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($result)){
$allimages[] = $row['uploader'];
}
print_r($allimages);
You should just be able to do:
$allImages[] = $row1;
This should just push the new array to the end of allImages array everytime the loop runs.

Displaying users with a php, sql count?

I have a database with users input and was wanting to output a user table (id, username) as a count on a page. The following piece of code is what I've been trying to work with but I've been having no luck and it keeps getting more and more complex - the SQL works perfectly so I'm not sure what's wrong.
mysqli_select_db($db);
$result = $_POST ['$result'] ;
$result = mysqli_query("SELECT COUNT( * )
FROM users");
$row = mysqli_real_escape_string($result,$db);
$total = $row[0];
echo "Total rows: " . $total;
I'm still learning how to properly link SQL in with PHP. The warnings tell me to add an extra parameter however when I do so it still complains.
I originally wanted a simple COUNT but will change the count to a table array if need be. I understand this maybe a little basic and I may have been going about it the wrong way, but I've hit a wall with it and any help on fixing the COUNT would be greatly appreciated
Replace the call to mysqli_real_escape_string to mysqli_fetch_array and your code will works.
mysqli_real_escape_string is only useful for string escaping when you INSERT or UPDATE data to MySQL.
$row = mysqli_fetch_array ($result);
Please try this code:
$sql="SELECT * FROM users";
$result=mysqli_query($con,$sql);
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
$number = count($rows);
Hope this works.

PHP Retrieve Database Value Error

So i have this so far..
if(isset($_POST['Decrypt']))
{
$dbinary = strtoupper($_POST['user2']);
$sqlvalue = "SELECT `value` FROM `license` WHERE `binary` = '$dbinary'";
$dvalue = mysql_query($sqlvalue) or die(mysql_error());
$dvalue = mysql_fetch_array($dvalue);
$dvalue['value'];
}
I have a field where the user enters a binary code which was encrypted. (The encrypt part works). This is supposed to retrieve the value from the database. When ever i do it, instead of the value showing up, it says "Array".
Please help me out.
This is because you can't just echo an array. You need to use functions like var_dump(); or print_r();
looks like you have more than one row in your table matching criteria. Try using while loop to retrieve data.
while($row = mysql_fetch_assoc($dvalue)){
//$row['value'];
}

php checking array values

Hey all i am trying to figure out a way to see what all values are within the array:
$sql = "select * from product where type = ? limit 1";
$query = self::$__CONN__->prepare($sql);
$query->execute(array($type)) or die(print_r($query->errorinfo()));
I am new at PHP and im not sure what all that is doing to populate the query string. I am guessing that the first query is connecting to the server and sending out the query but how do i see what its returning back in the array above?
Any help would be great!
Just dump the $type variable
var_dump($type);
Or if you are interested in the array wrapper itself
var_dump(array($type));

Categories