Problem with a SELECT WHERE query - php

Got a relatively simple MySQL query that I'm pulling using php with the following code:
$employeeNames = mysql_query(
"SELECT *
FROM employees
WHERE team=\"1st Level Technical Support_a\"
LIMIT 0,5000") or die(mysql_error());
$employeeNumRows = mysql_num_rows($employeeNames);
echo $employeeNumRows;
while ($row = mysql_fetch_array($employeeNames, $employeeNumRows)) {
echo $row['full_name'];
}
Now, if I run the query on the first line in SQL it gives me 18 results. If I echo $employeeNumRows it prints 18. Nothing else after that though.
If I change "1st Level Technical Support_a" to any other team in the table, it will bring up the proper results using PHP
This is the weirdest problem I've come across using MySQL/PHP - can anyone help? Has anyone seen something like this before?

Try removing the second parameter from your call to mysql_fetch_array, so that it reads mysql_feetch_array($employeeNames). See the documentation of the function to see how to use it properly.

Related

PHP echo of SQL query

I've a problem with my code. I can't wrap my head around what's wrong with the code or where I fail.
I basically want to calculate the average of multiple numbers from my database from one column.
/*data calculation*/
$calculation = "SELECT AVG(current) FROM offset_items";
$calculation_result = mysqli_query($connect, $calculation);
if($average = mysqli_fetch_assoc($calculation_result))
{echo "Average: ".$average["current"]."<br>";}
The problem here is, that I get only a blank displayed after the "Average: ".
But if I remove the AVG from the query, I get all results listed of that DB column. The DB contains data, which I import in the same script, as well as displaying them for testing.
I get the following error, which is the line of the echo:
Notice: Undefined index: current in C:\MAMP\htdocs\Offset_Items.php on line 50
I think the problem here is the mysqli_fetch_assoc, but I'm not sure.
Digging through the multiple topics about that "Undefined index" didn't solve my problem. Even when I declare the variable before as NULL.
I'm out of touch with programming, but currently picked it up again to code some tools for work. So if you guys could help me, I would be really thankful.
The PHP code that attempts to print the result reads:
$average = mysqli_fetch_assoc($calculation_result);
echo "Average: ".$average["current"]."<br>";
but the query that generates the result set does not contain any column named current.
Use an alias for the expression AVG(current) in the SELECT clause to get a column in the result set having the desired name:
SELECT AVG(current) AS current FROM offset_items

php weird issue when calling from web browser

I'm currently developing a video game, and experimenting a weird behavior trying to get the MAX value of a table in my database.
"select MAX(challengeID) AS challengeID from Challenges"
When I execute the sentence from phpMyAdmin all is going as expected, but when I call it from a web browser, I have to query twice in order to get the right answer. First time I call it from browser is returning the last MAX value before updating the table... maybe something related to cache¿
Edit:
There is all the PHP code (I think it is not a code issue, since it is the easiest query in the project...)
PHP code:
<?php
# connection stuff
$query = "select MAX(challengeID) AS challengeID from Challenges";
$result = mysql_query($query) or die('Query failed: ' .mysql_error());
$row = mysql_fetch_array($result);
echo $row['challengeID'];
?>
More info: I also tried this query with exactly the same issue
select challengeID from challenges group by challengeID order by challengeID desc
Thanks!
Carlos
So, I finally got the solution. Adding a hash code to the url is returning the correct value at the first call. The hash has to be different from the last hash used, but every two calls it is reseted ( I dont know why... I have to open a new question¿ ).
Thanks for your help! And #u_mulder take a tea please :)

Using $_GET to get search term, turn array into requested column [duplicate]

This question already has answers here:
fetch single record using PHP PDO and return result
(2 answers)
Closed 6 years ago.
I have not made the change from MySQL_ to PDO until today. Needless to say, the migration is more than a simple headache. So, I need a bit of help. I tried all the search terms I could before registering and asking this question.
My Problem
User types a numeric code into the search box, translates it to
.php?code=term
Script selects all columns from the database where the code is the
code term searched for.
PHP will Echo the results
My Code
if (isset($_GET["code"])) {
//IF USER SEARCHES FOR CODE, RUN THIS. ELSE SKIP.
$crimecode = $_GET["code"];
$crcode = $link->prepare("SELECT * FROM crimecodes WHERE code = :code");
$crcode->bindParam(':code', $crimecode);
$crcode->execute();
$coderesult = $crcode->fetchAll();
echo "<h4>CODE:</h4>";
echo $crimecode;
echo "<br /><h4>DEFINITION:</h4>";
echo $coderesult;
die();
}
Before, it was simple. All I had to do was:
$qcode = mysql_query("SELECT * FROM crimecodes WHERE code = $crimecode");
$fcode = mysql_fetch_assoc($qcode);
echo $fcode['definition'];
But, the ever evolving world has decided to fix something that wasn't broken so now the whole prior code is pointless and you gotta learn something new. Any help is appreciated to get this to work.
Right now, the above PDO code returns definition: ARRAY.
Like literally, the $coderesult prints Array.
The fetchAll() option returns an array containing all of the result set rows (http://php.net/manual/pt_BR/pdostatement.fetchall.php).
$coderesult prints Array because it's actually an array. If you do var_dump($coderesult) you'll see it.
I suppose that you are trying to get one row only. If that's the case, add this line after $coderesult = $crcode->fetchAll();:
$coderesult = $coderesult[0];
Then you can
echo $coderesult['definition'];
If you're trying to get more than one row, you need to use foreach to loop through the array.
I suggest you read the php manual for PDO Class or mysqli, wherever you prefer. There's a lot more options than mysql_.
Also, I think it's worth to mention that your previous code
$qcode = mysql_query("SELECT * FROM crimecodes WHERE code = $crimecode");
$fcode = mysql_fetch_assoc($qcode);
echo $fcode['definition'];
it's vulnerable to SQL Injection.

Basic php - echo mysql, how?

I want it to echo how many posts there are in bestallt where its ID is 1 from the table order and display it as how many posts there are in numbers. I am connected to the database in the PHP file, that's not an issue. I'm just not sure how to put it all down in PHP, quite new to this. I just can't get it to echo what I want, nothing comes out/I get an error.
Any help is appreciated!
Your present code does not fetch the data from the database, it simply echoes the SQL query you have written.
Quite a lot more code goes in to fetching results from a mysql database, and I am not sure reproducing a full explanation here will serve anyone's interests. However, you may wish to view the examples of how to use PDO (a method of using mysql databases from php) here, and then either edit or re-ask your question if you find you have specific difficulties following those examples.
Try this code:
<td>
<?php
$sql = "SELECT count(*) FROM order WHERE bestallt='1'"; // Your SQL query
$response = mysql_query($sql);
if (mysql_num_rows($response) == 0) {
// Your query returned 0 rows!
}
while($row = mysql_fetch_array($response)){
// For each row returned from your query
// $row is an array
// For example, You can use it:
echo $row['data1'];
echo $row['data2'];
echo $row['data3'];
// data1, data2, data3 are the name of the fiels in your database
}
?>
</td>
Have a nice day!
Try this,
$sql = "SELECT count(*) FROM order WHERE bestallt='1'"
$res=mysql_query($sql);
$arr=mysql_fetch_array($res);
echo "<pre>";print_r($arr); //you will get whole array for matching record of your order table if found anything
To learn more, you can check my earlier Answer: how generate report between the two dates using datepicker,ajax,php,mysql.?
In that answer I have described whole working demo.

php mysql result coming back wrong, every time from php, but is fine in sql pro and phpmyadmin

The following statement is returning a 1 in php. I've serialized and output the result and every other credential, and it is doing this on three seperate queries. When I run the query in sqlpro or phpmyadmin I get the result as 8. Please tell me someone has a bright idea.
$numRFPsSwitch = 0;
$bquery = " SELECT COUNT(rp.id) AS 'NumRFPs'
FROM rfp_proposal rp
WHERE rp.vendor_id = 1 AND rp.id IN(13,15,16,23,24,26,4,9) ";
$bresult = mysql_query($bquery, $connection);
$XMLFormatedString .= 'NumRFPs="';
while ($brow = mysql_fetch_object($bresult)){
$XMLFormatedString .= $brow->NumRFPs;
$numRFPsSwitch = 1;
}
What's returning 1? $numRFPsSwitch which is set to 1? :) $XMLFormatedString should have the correct value, your example works fine for me.
The result will always be a single row. So the loop should be replaced with an if.
Concerning the problem: Did you check your $connection?
Perhaps you could do a SELECT * (to return all rows involved), and then do a mysql_num_rows(...) on the result to find out the row count. If this returns the same result, then at least you can be sure it's not the query or concatenation that's at fault.
$query = "SELECT ...";
echo mysql_num_rows(mysql_query($query, $connection));
Are you sure you use the right login credentials in mysql_connect() and mysql_selectdb()?
Also: If you dó use the right credentials, are you sure you have the right permissions to select/update/delete etc.

Categories