I can query one table, not another, yet they are on the same DB.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 538 Views - Last Post: 13 February 2012 - 01:43 PM Rate Topic: -----

Topic Sponsor:

#16 MattRidge  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 27-January 12

Re: I can query one table, not another, yet they are on the same DB.

Posted 06 February 2012 - 09:37 AM

View PostJackOfAllTrades, on 06 February 2012 - 09:21 AM, said:

"It doesn't work...that's not good enough"

Could it "not work" because...you're not actually RUNNING the QUERY?


Umm, as far as I know I am running the query.

Also, just for a heads up, I don't appreciate people telling me that I don't read what is going on before saying it doesn't work.

Hence my code here:

echo "Query is : " . $query2 . "<br />";



That is as far as I can tell requesting the query...if it's not then please enlighten me, but honestly taking the attitude you have I'm not sure I really want your help because honestly I've asked for help and someone posted an answer in where they were introducing a new line of code.

He explained how it would work, and I asked if it would work under a specific situation. You came back and slapped your attitude into my face. If i wanted that I'd call my family.

I would rather know how something works in situations instead of just accepting an answer blindly. If you wish to help then please drop the attitude, I am asking questions in honesty, you are giving glib responses. That is not helpful.

So if you wish to actually help, and not troll then please do so otherwise ignore this topic, I don't need your kind of help.
Was This Post Helpful? 0
  • +
  • -

#17 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1760
  • View blog
  • Posts: 2,693
  • Joined: 08-June 10

Re: I can query one table, not another, yet they are on the same DB.

Posted 06 February 2012 - 12:44 PM

View PostMattRidge, on 06 February 2012 - 01:14 PM, said:

http://kaboomlabs.co...ed/box.php?id=1

So if I enter the code:

if(isset($_GET['id']))
    $id= mysqli_real_escape_string($dbc, trim($_GET["id"]));




Will this work?:

SELECT * FROM user
JOIN testbed
	ON testbed.fab1= user.userid
WHERE testbed.id = "$id"


Assuming the testbed.fab1 column is a reference to the user.userid column, then yes I can't see why it wouldn't. Just make sure the script stops, or sets a default value for $id, if your isset($_GET['id']) fails.

A great way to test queries is to use things like MySQL Workbench or phpMyAdmin to execute the queries directly on the database. Then you don't have to put them into a script until you are satisfied that they will actually work.

View PostMattRidge, on 06 February 2012 - 04:37 PM, said:

Umm, as far as I know I am running the query.

No, you never actually execute the query. To do that you would call the mysqli_query function (or $dbc->query(), if you are using the OOP version). You basically do:
// Connect to the database.
$dbc = new mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
    die("MySQL failed to connect: " . mysqli_connect_error());
}

// Create the SQL query
$sql = "SELECT stuff FROM table";

// Execute the SQL query and store the result set in
// the $result variable.
$result = mysqli_query($dbc, $sql) or die("Failed to execute query: " . mysqli_error($dbc));

// Read the results.
while ($row = mysqli_fetch_assoc($result)) {
    echo $row["stuff"];
}

// Free the result set.
mysqli_free_result($result);


Note that this is the old-school mysql_* API compatible style of using MySQLi. There is also an OOP version. See the link I posted above if you want to see an example of that.

Also, if you are going to be using user input, you may want to consider looking into using Prepared Statements.
Was This Post Helpful? 1
  • +
  • -

#18 MattRidge  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 27-January 12

Re: I can query one table, not another, yet they are on the same DB.

Posted 07 February 2012 - 05:18 AM

View PostAtli, on 06 February 2012 - 12:44 PM, said:

View PostMattRidge, on 06 February 2012 - 01:14 PM, said:

http://kaboomlabs.co...ed/box.php?id=1

So if I enter the code:

if(isset($_GET['id']))
    $id= mysqli_real_escape_string($dbc, trim($_GET["id"]));




Will this work?:

SELECT * FROM user
JOIN testbed
	ON testbed.fab1= user.userid
WHERE testbed.id = "$id"


Assuming the testbed.fab1 column is a reference to the user.userid column, then yes I can't see why it wouldn't. Just make sure the script stops, or sets a default value for $id, if your isset($_GET['id']) fails.

A great way to test queries is to use things like MySQL Workbench or phpMyAdmin to execute the queries directly on the database. Then you don't have to put them into a script until you are satisfied that they will actually work.

View PostMattRidge, on 06 February 2012 - 04:37 PM, said:

Umm, as far as I know I am running the query.

No, you never actually execute the query. To do that you would call the mysqli_query function (or $dbc->query(), if you are using the OOP version). You basically do:
// Connect to the database.
$dbc = new mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
    die("MySQL failed to connect: " . mysqli_connect_error());
}

// Create the SQL query
$sql = "SELECT stuff FROM table";

// Execute the SQL query and store the result set in
// the $result variable.
$result = mysqli_query($dbc, $sql) or die("Failed to execute query: " . mysqli_error($dbc));

// Read the results.
while ($row = mysqli_fetch_assoc($result)) {
    echo $row["stuff"];
}

// Free the result set.
mysqli_free_result($result);


Note that this is the old-school mysql_* API compatible style of using MySQLi. There is also an OOP version. See the link I posted above if you want to see an example of that.

Also, if you are going to be using user input, you may want to consider looking into using Prepared Statements.


Ok, but how will this work with the script I posted above? Do I do this before or after this script?
Was This Post Helpful? 0
  • +
  • -

#19 MattRidge  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 27-January 12

Re: I can query one table, not another, yet they are on the same DB.

Posted 13 February 2012 - 01:43 PM

Ok, this queries both fields, the issue I am running into though is this...

Although this queries user, it doesn't show the right name. What I need to do, and I don't know how to right now, is how to make it show the name that it is meant to... which is 2 Andy Khal. Can someone help and show me the code that is required? Thanks.

As it stands now,

<?php
// Connect to the database.
require_once('tb/connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
    die("MySQL failed to connect: " . mysqli_connect_error());
}

// Create the SQL query
$testbed = "SELECT * FROM testbed";
$user = "SELECT * FROM user";


// Execute the SQL query and store the result set in
// the $result variable.
$testbed = mysqli_query($dbc, $testbed) or die("Failed to execute query on tesbed table: " . mysqli_error($dbc));
$user = mysqli_query($dbc, $user) or die("Failed to execute query on user table: " . mysqli_error($dbc));

// Read the results.
$row = mysqli_fetch_assoc($testbed);
if(!$row)
{
  echo 'Query failed<br />';
}
else
{
	echo "Query for Testbed Fabricator is : " . $row["fab1"] . "<br />";
}
$row = mysqli_fetch_assoc($user);
if(!$row)
{
  echo 'Query for Testbed Fabricator failed<br />';
}
else
{
	echo "Query for User ID # is : " . $row["userid"], $row["user"] . "<br />";
}


// Free the result set.
mysqli_free_result($testbed);
mysqli_free_result($user);

?>


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2