Beginner noob, that's me

crazyal

Maddy
Hi There
Let me start by apologizing in advanced as I am truly LESS than a NOOB (is that even possible?) and I am trying to help out a friend who has some knowledge of PHP scripting. We have an SQL database set up and we need to setup a simple PHP program to access the data BUT with an index key (forgive me if I use the wrong terminology or lingo). At the moment his script searches in a sequential fashion which I am sure everyone will agree with me, is the worst way to access a file unless it's for something like a report where sequential is fine. Can someone provide me a simple example of a PHP script that would access a SQL DB using a key....OR.....point me to a thread that might have some SIMPLE, USEFULL sample that could help, please? This would be VERY VERY much appreciated.
Thank you in advance
 
Sure, I can help you with that! Here is an example of a simple PHP script that accesses an SQL database using a key:

PHP:
```php
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";


$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


// Define the key
$key = 1;


// Select data from the database using the key
$sql = "SELECT * FROM tablename WHERE id = $key";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}


$conn->close();
?>
```

In this script:
- Replace `localhost`, `username`, `password`, `dbname`, `tablename` with your own database details.
- Replace `id`, `name` with the column names from your table.
- Change the value of `$key` to the key you want to search for in the database.

This script will fetch the data from the database based on the key provided. I hope this helps! Let me know if you have any more questions.

Best Regard
Danish Hafeez | QA Assistant
ICTInnovations
 
WOW! That could just be what I need!
Thank you so much!



Sure, I can help you with that! Here is an example of a simple PHP script that accesses an SQL database using a key:

PHP:
```php
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";


$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


// Define the key
$key = 1;


// Select data from the database using the key
$sql = "SELECT * FROM tablename WHERE id = $key";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}


$conn->close();
?>
```

In this script:
- Replace `localhost`, `username`, `password`, `dbname`, `tablename` with your own database details.
- Replace `id`, `name` with the column names from your table.
- Change the value of `$key` to the key you want to search for in the database.

This script will fetch the data from the database based on the key provided. I hope this helps! Let me know if you have any more questions.

Best Regard
Danish Hafeez | QA Assistant
ICTInnovations
 
Sure, I can help you with that! Here is an example of a simple PHP script that accesses an SQL database using a key:

PHP:
```php
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";


$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


// Define the key
$key = 1;


// Select data from the database using the key
$sql = "SELECT * FROM tablename WHERE id = $key";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}


$conn->close();
?>
```

In this script:
- Replace `localhost`, `username`, `password`, `dbname`, `tablename` with your own database details.
- Replace `id`, `name` with the column names from your table.
- Change the value of `$key` to the key you want to search for in the database.

This script will fetch the data from the database based on the key provided. I hope this helps! Let me know if you have any more questions.

Best Regard
Danish Hafeez | QA Assistant
ICTInnovations
ok so for some reason it's not working. Is there any info I can provide you that could help u determine what is wrong? Oh I should mention the key I am searching with is a string not a numeric. and it's 36 characters long. Now on the database end I set the variable as KEY and INDEXED. Is that correct?
 
Back
Top