0

Hi.
I've been looking for a code, and found something, edited it a little bit and came up with nothing, so here's the original version of it:

<?php
$dbLink = new mysqli('localhost', 'usr', 'pwd', 'dbName');
if(mysqli_connect_errno()) {
    die('Failed to connect to MySQL: ' . mysqli_connect_error());
}

$sql = "SELECT `image_path`, `image_name` FROM `images`";
$result = $dbLink->query($sql);

if($result)
{
    while($row = $result->fetch_assoc())
    {
        echo "<img src='{$row['image_path']}' alt='{$row['image_name']}' /><br>";
    }
}
else
{
    echo "MySQL query failed: " . $dbLink->error;
}
?>

What I need to do is get the data from a single SQL table and put it from appropriate fields to appropriate columns of the table.

I will also need to put certain text, if a value in a certain column is that... For example:
if value = 2 {print 'JACK'}
else if value = 3 {print 'BILL'}

I'm not into web programming, sorry, I'm more into computer programming, but I need to make my site to show the data, so I'm not trying to learn the whole language, just a part of it to make it work.
If you have questions, for example I didn't explained it well, feel free to ask.

Thanks!

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by newn01
0

If you have programming experience, then you know how to do some debugging. You haven't identified if anything is working and just dumped some code.

A few comments:
You didn't define "...coming up with nothing." Does that mean a blank page, an error message or something else?

On line 2, the real program presumably uses the real id, pw and database name. Presumably you aren't getting a "Failed to connect" message.

You are using a class to access the database that code isn't included here. We can't know for sure if that is working correctly or not. You need to determine if you are getting a result at line 8 and if it is then ending up at the while loop at line 12.

If you are getting to line 14, I'm not sure that it will work. You have single quotes embedded within another set of single quotes embedded within double quotes with some braces thrown in for good measure. I suggest that you keep it simple as follows just in case this is a problem:

echo "<img src=".$row['image_path']." alt=".$row['image_name']."><br>";

Edited by chrishea: n/a

0

Sorry, wrong code... Just copied from a link, thought I selected the right one. Sorry.
In the meantime, I have found some other code and edited it a bit. It works, but I don't know, how to get the information from a column and put it into a table column... From specific column into certain table column, not a for loop, which makes all columns to appear...
I've tried few lines of same code, it is supposed to get column, but I guess, some of it is specific commands, not only declarations, or I did a mistake somewhere. Here's the code:

<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'IP';
$db_user = 'USER';
$db_pwd = 'PASS';

$database = 'DB';
$table = 'TABLE';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result1 = mysql_query("SELECT steam_id FROM {$table}");
$result2 = mysql_query("SELECT gag FROM {$table}");
/*
if (!$result) {
    die("Query to show fields from table failed");
}
*/
echo "<h1>Mutai:</h1>";
echo "<table border='1'><tr>";
// printing table headers
    $field1 = mysql_fetch_field($result1);
    $field2 = mysql_fetch_field($result2);
    echo "<td>Steam ID</td>";
    echo "<td>Muto tipas</td>";
    echo "<td>Muto trukme</td>";
    echo "<td>Gago tipas</td>";
    echo "<td>Gago trukme</td>";
    echo "<td>Muto uzdejo</td>";
    echo "<td>Gaga uzdejo</td>";
echo "</tr>\n";
// printing table rows
while($row1 = mysql_fetch_row($result1))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row1 as $cell1)
        echo "<td>$cell1</td>";

    echo "</tr>\n";
}
while($row2 = mysql_fetch_row($result2))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row2 as $cell2)
        echo "<td>$cell2</td>";

    echo "</tr>\n";
}
mysql_free_result($result1);
mysql_free_result($result2);
?>
</body></html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.