Jedijon Posted November 21, 2018 Share Posted November 21, 2018 $result = mysqli_query($con,"SELECT * FROM $_SESSION['datakey']"; Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 You would use an INSERT query. A SELECT query retrieves data. Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 Like this $result = mysqli_query($con,"INSERT * FROM $_SESSION['datakey']"; Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 No, not like that. You need the correct insert query syntax or there is tutorial here Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 I don't wan't it to be inserted into the database but use the session info as the name of the table to grave data from for the html table Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 you need to put {..} around the array element inside a string $result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 $result = mysqli_query($con,"SELECT * FROM table name"); but where the table name goes I wan't it use $_SESSION['datakey'] to get the table name Link to comment Share on other sites More sharing options...
mac_gyver Posted November 21, 2018 Share Posted November 21, 2018 this sounds like a bad design. Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 Didn't work $result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 It will grave the info from the login SESSION Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 Define "didn't work" . That tells us nothing. What does $_SESSION['datakey'] contain? Is it the name of a table? What is your code after you get the $result? We are not telepathic! Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 $_SESSION['datakey'] contains the name of the table Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 24 minutes ago, mac_gyver said: this sounds like a bad design. I couldn't agree more. Variable tables names always rings alarm bells Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 1 minute ago, Jedijon said: $_SESSION['datakey'] contains the name of the table OK, so that leaves Define "didn't work" . That tells us nothing. What is your code after you get the $result? Link to comment Share on other sites More sharing options...
benanamen Posted November 21, 2018 Share Posted November 21, 2018 I know OP, tell us what the Real problem is you are trying to solve rather than asking about the poorly implemented attempt at solving the problem. What is the overall task at hand? Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 <?php $con=mysqli_connect("IP","Username","Password","Database name"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; echo "<table border='1'> <tr> <th></th> <th></th> <th></th> <th></th> <th></th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['column1'] . "</td>"; echo "<td>" . $row['column2'] . "</td>"; echo "<td>" . $row['column3'] . "</td>"; echo "<td>" . $row['column4'] . "</td>"; echo "<td>" . $row['column5'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 Try putting session_start() right at the top of your code. You need that to access session data Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 I have that <?php // Initialize the session session_start(); // Check if the user is logged in, if not then redirect him to login page if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){ header("location: ../login"); exit; } ?> Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 Error message Parse error: syntax error, unexpected ';', expecting ',' or ')' in /srv/disk1/2648096/www/site/test/index.php on line 20 Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 PS Before you connect to mysql, call mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); That will force it to report errors. Link to comment Share on other sites More sharing options...
benanamen Posted November 21, 2018 Share Posted November 21, 2018 Notice anything wrong here? $result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; Link to comment Share on other sites More sharing options...
ginerjm Posted November 21, 2018 Share Posted November 21, 2018 You should write query statements into a variable to make it easier to debug should you need to, as in this case. $q = "SELECT * FROM {$_SESSION['datakey']}"; echo "Query looks like: $q"; $results = mysqli_query($con, $q); Run this code and look at the actual query you created to ensure that your assumptions are correct. Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 3 minutes ago, Jedijon said: I have that Not in the code you showed us Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 Query looks like: SELECT * FROM datakeyWarning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in /srv/disk1/2648096/www/site/test/index.php on line 35 It shows the session data Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 It works Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.