Trying to combine multiple queries into one query - php

*Note, if you can think of a more constructive title, please do edit.
I have a table that looks like this:
id | forGame | otherType | otherName | otherDesc
9 |+Stellar+Dawn |Character |Car |Car
10 |+Stellar+Dawn |Item |Brugson Burson |a guy
11 |+Stellar+Dawn |Item |Space Pie |A pie from space
Now, my problem is that I'm trying to seperate multiple queries into one:
$gameOther_typeItem = $database->queryDB("SELECT * FROM `mainSite_others` WHERE otherType='Item' AND forGame='$gameName'");
$gameOther_typeEntity = $database->queryDB("SELECT * FROM `mainSite_others` WHERE otherType='Entity' AND forGame='$gameName'");
$gameOther_typeCharacter = $database->queryDB("SELECT * FROM `mainSite_others` WHERE otherType='Character' AND forGame='$gameName'");
(Don't worry about the $database->... part, that's my class.)
How might I combine these into one?
Thank you.

You could use "`OR'".
SELECT * FROM my_table WHERE column1 = value1 AND column3 = value3 OR column2 = value2 AND column3 = value3
Or, you could use multiple query to execute multiple queries at the same time?
http://php.net/manual/en/mysqli.multi-query.php
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
?>

SELECT * FROM mainSite_others WHERE (otherType='Item' OR otherType =
'Entity' OR otherType = 'Character') AND forGame='$gameName';

Related

If else statement with mysql table? - PHP

I need to make an if else statement that will prevent a user from bidding on an item if they are the highest bidder. I think it would go a little something like this.
if ($accountid = the accountid in the bidhistory table)
{
echo "You are the highest bidder!";
}
else
{
$sql="INSERT INTO bidhistory (accountid, biditemid)
VALUES ($accountid, $itemid)";
mysql_query("
UPDATE bidhistory
SET bidprice = bidprice + 1
WHERE biditemid = " .
#mysql_escape_string($itemid));
$result=mysql_query($sql) or die("Error in adding bid for item: ".mysql_error());
}
?>
I'm not sure how to reference the accountid from the bidhistory table. Also, if there is a better way to do this, please point me in the wright direction. Thanks!
Note:
You can fetch the row of the highest bid with the specific item first.
Then check if the highest bidder (accountid) is the same with the current user.
You can check the notes I had for some lines quoted in /* ... */
Your query for checking:
$result = mysql_query("SELECT accountid FROM bidhistory WHERE biditem = '$itemid' ORDER BY bidhistoryid DESC LIMIT 1"); /* GET THE LAST ROW FOR WHO BIDS LAST; AND REPLACE NECESSARY COLUMN NAME (unique/primary) - bidhistoryid */
while($row = mysql_fetch_array($result)){
$checkaccountid = $row['accountid']; /* STORE THE USER THAT BIDS LAST FOR THIS ITEM */
}
if($checkaccountid == $accountid){ /* THEN COMPARE IT WITH THE CURRENT USER */
/* CODE YOU WANT TO DO IF HE/SHE IS THE LAST BIDDER ALREADY */
}
else {
/* IF NOT, HE/SHE CAN STILL BID */
}
But I recommend that you use mysqli_* rather than the deprecated mysql_* API.
$con = new mysqli("YourHost", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */
/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if($stmt = $con->prepare("SELECT accountid FROM bidhistory WHERE biditem = ? ORDER BY bidhistoryid DESC LIMIT 1")){
$stmt->bind_param("i",$itemid); /* BIND THIS VARIABLE TO YOUR QUERY ABOVE */
$stmt->execute(); /* EXECUTE THE QUERY */
$stmt->bind_result($checkaccountid); /* STORE THE RESULT TO THIS VARIABLE */
$stmt->fetch(); /* FETCH THE RESULT */
if($checkaccountid == $accountid){
/* CODE YOU WANT TO DO IF HE/SHE IS THE LAST BIDDER ALREADY */
}
else {
/* IF NOT, HE/SHE CAN STILL BID */
}
$stmt->close();
} /* END OF PREPARED STATEMENT */

PHP and MySQL - Get number of specific results from column

I have this code for getting how many results there are in results there are in a column:
$result = "SELECT name FROM users";
echo $result;
How can I display a certain number of a specific string? For example, I want to search for the name "Andrew" in the column "name". There are 20 results for "Andrew". How can I echo out "20"?
You would COUNT them:
$result = "SELECT COUNT(`name`) AS `NameCount` FROM `users` WHERE `name` = 'Andrew'";
For all names you could do this:
SELECT `name`, count(`name`) AS `NameCount`
FROM `users`
GROUP BY `name`
Which would result in something like this:
name | NameCount
----------------------
Andrew | 20
Bob | 6
Carol | 125
If you will use Mysqli, you can use $num_rows. Somelithing like:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT Code, Name FROM Country ORDER BY Name")) {
/* determine number of rows result set */
$row_cnt = $result->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
/* close result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
also, you can use mysql count() function like:
$result = "SELECT count(*), name FROM users";

how to get all matching records from mysql database using php?

I have the following table set in my mysql database
mem_id | pid | content
0 | 1 | All the content is here
0 | 2 | All the content is here
0 | 3 | All the content is here
Now the problem is to get all matching mem_id values and store it in a array in php.
Example: A variable called $id has value 0
So now I have to get all values under the column content but only those which matches the mem_id of the user.
Could anyone help me with this, I need it in php and using mysql query to get all the values.
My current code:
$con=mysqli_connect("localhost","*****","******","*****");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user_friends WHERE mem_id = '$_SESSION[SESS_MEMBER_ID]' LIMIT 1");
while($row = mysqli_fetch_array($result))
{
$friends = $row['fid'];
}
SELECT * FROM yourTABLE WHERE mem_id=0 // or 1 or 2 or 3 etc
Using PHP you could query it like:
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$id = intval($id); // Put your ID here
$query = "SELECT * FROM yourTABLE WHERE mem_id=$id";
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
mysqli_free_result($result);
}
?>
$query="select * from TABLE_NAME where `mem_id`='$id'";

php echo data from certain id

EDIT: I'm sorry I was unclear, I try to explain it right this time.
I have this data in a database table called tMenu:
id page_nl text
1 index_1 index1_text
2 index_2 index2_text
3 index_3 index3_text
These are 3 pages on my website called (in this case) index_1, index_2 and index_3. I have programmed it is such a way that each page shows there index1_text.
What I want now is to show page_nl in a menu. The code I have now is:
$qh = mysql_query('SELECT id, page_nl FROM tMenu ORDER BY id');
$row = mysql_fetch_array($qh);
$id = 'id';
<? echo $row['page_nl']; $id=="1" ;?>
<? echo $row['page_nl']; $id=="2" ;?>
<? echo $row['page_nl'];?>
In the way it is now it shows only page_nl from id 1, but I want that the next link shows page_nl from id 2. I hope my question is more clear now.
Your question isn't very clear - are you asking for something like this
$sql = "select * from yourtable where id = 1";
$result = mysql_query($sql);
//I am assuming there are more than 1 rows for ID 1
while($row = mysql_fetch_assoc($result)) {
echo $row['page_nl'];
}
OR ============================
$sql = "select * from yourtable"; //Select All
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
if($row['id'] == 1)
{
echo $row['page_nl'];
}
}
Presuming you mean database table, you need a routine to connect to the database then fetch the info:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "databasename"); // database name
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM table_name"; // put table name here
$result = $mysqli->query($query);
/* numeric array */
/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["id"], $row["page_nl"]);
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
?>
You need to use a foreach($var as $key =>$value) loop

query multiple rows in mysql as variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - Simple way to read single record from MySQL
I want to query multiple rows in mysql as variable. for example:
SELECT name, tid FROM term_data WHERE vid = 2
this is the result:
name | tid
-----|----
Jack | 55
Tony | 87
John | 32
then I want to use while:
while (...) {
print "My name is: $name and my ID is: $tid";
//name and tid should be printed from database.
}
I can query one row and put it in a while but How is this possible?
Example from the PHP manual:
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
<?php
//Conection
...
$query= "SELECT name, tid FROM term_data WHERE vid = 2";
if (query_run=mysql_query($query)) {
while ($query_row=mysql_fetch_assoc($query_run)) {
$name=$query_row['name'];
$tid=$query_row['tid'];
echo 'My name is'.$name.'and my ID is'.$tid;
}
}
else {
echo mysql_error();
}
?>
Only thing remains is resolving the connection which is up to you.

Categories