PHP/SQL SELECT statement using asterisk - php

I am having trouble getting an sql request to work. Without giving more details than needed,
$db_query = mysql_query(" select years,avg,best,win,top10,champs from `profile` where PLAYERID = '$monkey_id'");
works fine. However,
$db_query = mysql_query(" select * from `profile` where PLAYERID = '$monkey_id'");
doesn't return any results. The only change is that I'm trying to pull all fields instead of just those few. I'm at a loss to explain this. I taught myself all this stuff, so it's always possible I'm doing something dumb.
Edit:
Here's the rest of the surrounding code:
$db_query_inside = mysql_query(" select * from `profile` where PLAYERID = $monkey_id");
$db_query = mysql_fetch_array($db_query_inside);
$years_prev = $db_query['years'];
$avg_prev = $db_query['avg'];
$best_prev = $db_query['best'];
$win_prev = $db_query['win'];
$top10_prev = $db_query['top10'];
$champs_prev = $db_query['champs'];
Edit again:
Still don't know why it wouldn't work with *, but I just got what I needed done by listing the specific fields. It doesn't end up with any sort of error that can be gleaned from
die(mysql_error())
so I'm just giving up and working on stuff that reacts rationally.

Why not try:
$db_query = mysql_query(" select `profile` where PLAYERID = '$monkey_id'");

Let's do this, modify the following line to reflect below. See what the error says, if any. I tried it myself (your code) and it seems to work fine.
$db_query_inside = mysql_query(" select * from `profile` where PLAYERID = $monkey_id") or die(mysql_error());

Related

Dynamically Query Mysql using PHP PDO and using % in Select

I have queried the database and got a list of database names that match my criteria. This is what I ran to get the information.
$sql = "SELECT DISTINCT tb.TABLE_SCHEMA
FROM
INFORMATION_SCHEMA.TABLES AS tb
INNER JOIN
INFORMATION_SCHEMA.TABLES AS tb2
WHERE tb.TABLE_NAME like '%OPTION%'
AND tb2.TABLE_NAME like '%USER%'";
$query = $handler->prepare($sql);
$query->execute();
$dbnames = $query->fetchAll(PDO::FETCH_COLUMN);
What I am trying to accomplish:
Now that I have the names, I need to run through each database so that I can get the list of websites, email of admin and user login. I know I can achieve this individually, but I am trying to do this dynamically with variables. The select statement below would run and give me my answer. However the prefix before the underscore in the FROM ysY6q8hmL7_options is different in each database.
SELECT option_value FROM `ysY6q8hmL7_options` WHERE `option_name` = 'home'
I tried this code but consistently get an error. How can I solve this problem. How can I run the queries dynamically without knowing the full name of each table. thank you.
foreach ($result as $val) {
$sql = "SELECT option_value FROM $val.%_options WHERE option_name = 'home'";
$query = $handler->prepare($sql);
$query->execute();
$dbnames = $query->fetchAll(PDO::FETCH_ASSOC);
printResultConsole($dbnames);
Your logic looks ok, but why do you have that percent sign in the table name ? That’s probably what is causing an error (whose message you should have shared, by the way).
Try this instead :
$sql = "SELECT option_value FROM " . $val . "_options WHERE option_name = 'home'";

Deleting records from MySQL inside foreach loop - PHP

I'm checking a project and I found this code that is suppose to delete records from MySQL, despite this is not the best aproach to achieve this I have to make this works, but for me is fine, that's why I'm here, because I don't see the error or what is wrong with this code, is not displaying errors, warning, nothing I tested the code directly in PHPMyAdmin and it works well, so this is the code...
$get_records = mysql_query('SELECT id_detail FROM my_table WHERE user_id = "'.$user_id.'" AND last_date BETWEEN "'.$date1.'" AND "'.$date2.'"') or die (mysql_error());
$array_details = array();
while ($details = mysql_fetch_array($get_records)) {
$array_details[] = $details['id_detail'];
}
$array_details = array_unique($array_details);
foreach ($array_details as $id_detail) {
$delete_detail = mysql_query("DELETE FROM my_table WHERE user_id = '".$user_id."' AND id_detail = '".$id_detail."'") or die (mysql_error());
}
I got two records in the same table with the same ID detail (this works that way), so If I get the ID correcly with the query inside the foreach loop it should delete all the records with that id_detail and user_id doesn't matter if in the query I say between which dates get those IDs, it deletes only one, and that one is which has the date between that range
I have no idea why is not deleting both, some idea about what I'm missing?
NOTE: As I said before if I do that query directly in PHPMyAdmin it works fine and if I do from PHP it delete only one.
Does replacing that code with this query work for you?
$delete_detail = mysql_query("DELETE FROM my_table WHERE user_id = '".$user_id."' AND id_detail IN (SELECT id_detail FROM my_table WHERE user_id = '".$user_id."' AND last_date BETWEEN '".$date1."' AND '".$date2."')") or die (mysql_error());

Updating a temporary table

I'm trying to update a temporary table then query it in a later statement but I don't seem to be having any look. Below is the code I am using:
$updatepostgrad = "UPDATE t1
SET AssociatedModule = 'COMP102'
ORDER BY StudentRanking DESC
LIMIT $numberofstudents";
$perform9 = mysql_query($updatepostgrad);
To update the temporary table. Then to query it I use the code below:
$qry15 = "SELECT * FROM t1 WHERE AssociatedModule = 'COMP102'";
$result15 = mysql_query($qry15);
Obviously the table was created successfully early on in the code. When I use
var_dump($result15);
to try and gather information all I get is bool(false). Any ideas whats going wrong.
In case it helps the idea of querying the table is to perform the below:
while ($return4 = mysql_fetch_assoc($result15)) {
$id = $return4['ID'];
$qry15 = "UPDATE postgraduate SET AssociatedModule = 'COMP102' WHERE ID = '$id'";
$perform15 = mysql_query($qry15);

PHP SQL Select From Where

I am having some difficulty running some SQL code.
What I am trying to do is, find a row that contains the correct username, and then get a value from that correct row.
This is my SQL in the php:
mysql_query("SELECT * FROM users WHERE joined='$username' GET name")
As you can see, it looks for a username in users and then once found, it must GET a value from the correct row.
How do I do that?
You need some additional PHP code (a call to mysql_fetch_array) to process the result resource returned by MySQL.
$result = mysql_query("SELECT name FROM users WHERE joined='$username'");
$row = mysql_fetch_array($result);
echo $row['name'];
mysql_query("SELECT `name` FROM users WHERE joined='$username' ")
Just select the right column in your 'select clause' like above.
Edit: If you are just starting out though, you might want to follow a tutorial like this one which should take you through a nice step by step (and more importantly up to date functions) that will get you started.
mysql_query("SELECT name FROM users WHERE joined='$username'")
$q = mysql_query("SELECT * FROM users WHERE joined='$username'");
$r = mysql_fetch_array($q);
$name = $r['user_name']; // replace user_name with the column name of your table
mysql_query("SELECT name FROM users WHERE joined='$username' ")
Read documentation : http://dev.mysql.com/doc/refman/5.0/en/select.html

PHP & MySQL: How can I use "SET #rank=0;" in $query=

In my PHP file, I use this line to pull data from my mySQL database:
$query = "SET #rank=0; SELECT #rank:=#rank +1 as rank, Blah Blah...";
If I check the SELECT statement in phpMyAdmin's SQL window (without $query= ) it works fine.
But, if I use it in PHP, then I get an error. It doesn't like the "SET #rank=0;" bit. Is there a way to use "SET #rank=0;" when it's in "$query=" ? Is there a workaround?
The rest of the code is standard stuff for pulling data from a db:
public function getmyData() {
$mysql = mysql_connect(connection stuff);
$query = "SELECT #rank:=#rank +1 as rank, formatted_school_name, blah blah";
$result = mysql_query($query);
$ret = array();
while ($row = mysql_fetch_object($result)) {
$tmp = new VOmyData1();
$tmp->stuff1 = $row-> stuff1;
$tmp->stuff2 = $row->stuff2;
$ret[] = $tmp;
}
mysql_free_result($result);
return $ret;
}
Update: I'm trying to use Amerb's suggestion of using multi-query. I concatenated the query like so:
$query = "SET #rank = 0";
$query .= "SELECT #rank:=#rank +1 as rank...
I changed the result to:
$result = $mysqli_multi_query($query);
But, it's failing for some reason. I'm on a machine running PHP 5.2. Any suggestions?
This guy here seems to have a way of setting the variable in the same query to zero. I don't have MySQL set on up on this machine to try it, though.
Here's the query he suggests in his blog post:
select #rownum:=#rownum+1 ‘rank’, p.* from player p, (SELECT #rownum:=0) r order by score desc limit 10;
(Is there some homework assignment coming due somewhere having to do with computing ranks? This is the third question I've seen on this in two days.)
Are you checking for duplicate scores?
Try executing it as 2 separate successive queries.
You have to enable the use of multiple queries in one, but i forgot how do do this at the moment. It's a security feature.
Use mysql_multi_query() or rather mysqli_multi_query() instead of mysql_query()

Categories